<?php

namespace App\Http\Controllers\Admin;

use App\Model\Admin\Admin as A;
use App\Model\Common\LoginLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class AdminController extends CommonController
{
    public function login()
    {
        return $this->fetch();
    }

    public function doLogin(Request $request)
    {
        $signname = $request->input('signname', '');
        $password = $request->input('password', '');
        $remember = $request->input('remember', 0);

        if (!$signname) {
            return $this->output(20020);
        }
        if (!$password) {
            return $this->output(20021);
        }
        $data = [];
        $data['signname'] = $signname;
        $data['status'] = 1;
        $Admin = new A();
        $admin = $Admin->where($data)->first();
        if ($admin) {
            $minutes = 60;
            $aid = $admin->id;
            $error_count = cache('login_error_count:'.$aid) ?: '0|0';
            // $error_count = cache(['login_error_count:' => ])
            list($error_count, $last_error_time) = explode('|', $error_count);
            if ($last_error_time + 3600 * 24 < time()) {
                // cache('login_error_count:'.$uid, null);
                cache(['login_error_count:'.$aid => null], $minutes);

                // session('login_need_captcha', null);
                session(['login_need_captcha' => null]);
                $error_count = 0;
            }
            if ($error_count >= 3 && !captcha_check($request->input('captcha'))) {
                if ($error_count >= 3) {
                    session(['login_need_captcha' => 1]);
                }

                return $this->output(20004, '', [
                    'login_need_captcha' => session('login_need_captcha') ?: 0,
                ]);
            }
            if (!$admin->checkPassword($password)) {
                // cache('login_error_count:'.$uid, ($error_count + 1) . '|' . time());
                cache(['login_error_count:'.$aid => ($error_count + 1).'|'.time()], $minutes);
                if ($error_count + 1 >= 3) {
                    session(['login_need_captcha' => 1]);
                }

                return $this->output(20003, '', [
                    'login_need_captcha' => session('login_need_captcha') ?: 0,
                    'captcha_src' => captcha_src(),
                ]);
            } elseif ($error_count > 0) {
                cache(['login_error_count:'.$aid => null], $minutes);
                session(['login_need_captcha' => null]);
            }
            // session('aid', $admin->id);
            session(['admin:aid' => $admin->id]);
            // session('gid', $admin->group_id);
            session(['admin:gid' => $admin->group_id]);
            session(['admin:signname' => $admin->signname]);
            $redirect = $request->input('redirect');
            if (!$redirect) {
                $redirect = route('admin/index');
            }

            $Log = new LoginLog();
            $Log->ip = $request->ip();
            $Log->uid = $admin->id;
            $Log->type = 2;
            $Log->save();

            return $this->output(0, '登录成功', [
                'aid' => $admin->id,
                'gid' => $admin->group_id,
                'nickname' => $admin->nickname,
                'signname' => $admin->signname,
                'redirect' => $redirect,
            ]);
        } else {
            return $this->output(20002);
        }
    }

    public function logout(Request $request)
    {
        $session = $request->session();
        $session->forget('admin:aid');
        $session->forget('admin:gid');
        $session->forget('admin:signname');
        if ($request->ajax() || (isset($_REQUEST['_async']) && 1 == $_REQUEST['_async'])) {
            return output(0, '', ['redirect' => route('admin/login')]);
        } else {
            return redirect()->route('admin/login');
        }
    }

    // private function

    // public function register()
    // {
    //     $data = [];
    //     $data['signname'] = 'admin';

    //     $data['password'] = md5('admin123');

    //     $Admin = new A();
    //     $Admin->signname = $data['signname'];
    //     $Admin->password = $data['password'];
    //     $re = $Admin->save();
    //     return $this->output(0,'', ['result' => $re]);
    // }
}
